home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / jade / src / unix_client.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  4KB  |  133 lines

  1. /* unix_client.c -- client program to communicate with unix_server.c
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with Jade; see the file COPYING.    If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <sys/un.h>
  28. #include <pwd.h>
  29.  
  30. #define JADE_SOCK_NAME ".jade_rendezvous"
  31.  
  32. int
  33. main(int argc, char *argv[])
  34. {
  35.     u_long linenum;
  36.     u_long result = 0;
  37.     argc--; argv++;
  38.     if(argc == 0)
  39.     {
  40.     fprintf(stderr, "usage: jadeclient { [+LINE-NUMBER] FILE-NAME }...\n");
  41.     return(0);
  42.     }
  43.     while(result == 0 && argc > 0)
  44.     {
  45.     result = 5;
  46.     if(argc >= 1 && **argv == '+')
  47.     {
  48.         linenum = strtol(argv[0], NULL, 0);
  49.         if(linenum <= 0)
  50.         linenum = 1;
  51.         argc--; argv++;
  52.     }
  53.     else
  54.         linenum = 1;
  55.     if(argc > 0)
  56.     {
  57.         char buf[512];
  58.         char *filename;
  59.         int sock_fd;
  60.  
  61.         /* Make sure the filename is absolute; the server could have
  62.            a different working directory to us.  */
  63.         if(**argv != '/' && **argv != '~')
  64.         {
  65.         char *end;
  66.         if(!getcwd(buf, 511))
  67.         {
  68.             perror("getcwd");
  69.             exit(5);
  70.         }
  71.         end = buf + strlen(buf);
  72.         if(end[-1] != '/')
  73.             *end++ = '/';
  74.         strcpy(end, *argv);
  75.         filename = buf;
  76.         }
  77.         else
  78.         filename = *argv;
  79.  
  80.         sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  81.         if(sock_fd >= 0)
  82.         {
  83.         struct sockaddr_un addr;
  84.         struct passwd *pwd = getpwuid(getuid());
  85.         if(pwd && pwd->pw_dir)
  86.         {
  87.             char *end;
  88.             strcpy(addr.sun_path, pwd->pw_dir);
  89.             end = addr.sun_path + strlen(addr.sun_path);
  90.             if(end[-1] != '/')
  91.             *end++ = '/';
  92.             strcpy(end, JADE_SOCK_NAME);
  93.             addr.sun_family = AF_UNIX;
  94.             if(access(addr.sun_path, F_OK) != 0)
  95.             {
  96.             /* Jade isn't running yet. hang around 'til it is... */
  97.             fprintf(stderr, "Jade not running, waiting...");
  98.             fflush(stderr);
  99.             do {
  100.                 sleep(1);
  101.             } while(access(addr.sun_path, F_OK) != 0);
  102.             fprintf(stderr, "okay\n");
  103.             }
  104.             if(connect(sock_fd, (struct sockaddr *)&addr,
  105.             sizeof(addr.sun_family) + strlen(addr.sun_path)) == 0)
  106.             {
  107.             u_short filenamelen = strlen(filename);
  108.             if(write(sock_fd, &filenamelen, sizeof(u_short)) != sizeof(u_short))
  109.                 perror("jadeclient:write():filenamelen");
  110.             else if(write(sock_fd, filename, filenamelen) != filenamelen)
  111.                 perror("jadeclient:write():filename");
  112.             else if(write(sock_fd, &linenum, sizeof(u_long)) != sizeof(u_long))
  113.                 perror("jadeclient:write():linenum");
  114.             else if(read(sock_fd, &result, sizeof(u_long)) != sizeof(u_long))
  115.                 perror("jadeclient:read():result");
  116.             }
  117.             else
  118.             perror("jadeclient:connect()");
  119.         }
  120.         else
  121.             fprintf(stderr, "jadeclient: can't find your home dir\n");
  122.         }
  123.         else
  124.         perror("jadeclient:socket()");
  125.     }
  126.     argc--; argv++;
  127.     /* a short pause is nice. */
  128.     if(argc > 0)
  129.         sleep(1);
  130.     }
  131.     return(result);
  132. }
  133.